<template>
  <div class="main_box">
    <el-row class="header-row">
      <el-col>
        <div class="search_wrap">
          <el-form :inline="true" :model="filters" class="demo-form-inline" ref="searchForm">
            <el-form-item prop="mchName">
              <el-input v-model="filters.mchName" clearable placeholder="商户号名称"></el-input>
            </el-form-item>
            <el-form-item prop="mchId">
              <el-input v-model="filters.mchId" clearable placeholder="商户号Id"></el-input>
            </el-form-item>
            <!-- <el-form-item prop="company">
                            <el-input v-model="filters.company" clearable placeholder="公司名称"></el-input>
                        </el-form-item> -->
            <el-form-item prop="institutionId">
              <el-select v-model="filters.institutionId" clearable filterable placeholder="医检机构">
                <el-option v-for="item in institutionList" :value="item.value" :label="item.label" :key="item.value"></el-option>
              </el-select>
            </el-form-item>
            <el-form-item>
              <el-button icon="el-icon-search" type="primary" @click="onSearch">查询</el-button>
              <el-button icon="el-icon-refresh-left" @click="resetForm">重置</el-button>
            </el-form-item>
          </el-form>
        </div>
      </el-col>
      <el-col>
        <div class="opration_wrap">
          <el-tooltip content="刷新" placement="top">
            <el-button circle icon="el-icon-refresh-right" @click="reload" size="mini"></el-button>
          </el-tooltip>
          <div>
            <el-button type="primary" plain icon="fa fa-plus" v-has="permissionAdd" @click="handleClickAdd()" size="mini"> 新增</el-button>
          </div>
        </div>
      </el-col>
    </el-row>

    <el-row ref="tableRow">
      <el-table :data="dataList" v-loading="listLoading" :fit="true" highlight-current-row>
        <el-table-column type="selection" width="50"></el-table-column>
        <el-table-column align="center" type="index" label="序号" width="50"></el-table-column>
        <el-table-column align="center" prop="mchName" label="商户号名称" width="200"></el-table-column>
        <el-table-column align="center" prop="mchId" label="商户号Id" width="200"></el-table-column>
        <el-table-column align="center" prop="mchPrivateKey" label="API密钥" width="200"> </el-table-column>
        <el-table-column align="center" prop="company" label="公司名称" width="200"></el-table-column>
        <el-table-column align="center" prop="institutionName" label="医检机构" width="200"></el-table-column>
        <el-table-column label="状态" align="center" width="100">
          <template slot-scope="scope">
            <span>
              <el-tag size="mini" effect="dark" :type="formatStates(scope.row.state)">
                {{ formatState(scope.row.state) }}
              </el-tag>
            </span>
          </template>
        </el-table-column>
        <el-table-column align="center" prop="createDate" label="创建时间"></el-table-column>
        <el-table-column align="center" prop="createUserName" label="创建人"></el-table-column>
        <el-table-column label="操作" align="center" width="200">
          <template slot-scope="scope">
            <el-dropdown trigger="click" size="small">
              <el-button type="primary"> <i class="fa fa-list-ul"></i>更多 </el-button>
              <el-dropdown-menu slot="dropdown">
                <el-dropdown-item v-has="permissionEdit" @click.native="handleClickEditor(scope.row.mchInfoId)">编辑</el-dropdown-item>
                <el-dropdown-item v-has="permissionUpStatus" @click.native="handleIsDisable(scope.row)"> {{ scope.row.state == 0 ? '启用' : '禁用' }}</el-dropdown-item>
                <el-dropdown-item v-has="permissionDelete" @click.native="handleClickDelete(scope.row)">删除</el-dropdown-item>
              </el-dropdown-menu>
            </el-dropdown>
          </template>
        </el-table-column>
      </el-table>
      <el-pagination
        background
        @size-change="handleSizeChange"
        @current-change="handleCurrentChange"
        :current-page="page.pageNo"
        :page-sizes="[20, 50, 100]"
        :page-size="page.pageSize"
        layout="total, sizes, jumper, next, pager, prev"
        :total="page.pageTotal"
      ></el-pagination>
    </el-row>
  </div>
</template>

<script>
import * as message from '@/constants/message';
import MerchantNoApi from '@/api/finance/merchantNo';
import InstitutionApi from '@/api/institution/institution';
export default {
  name: 'MerchantNoManagement',
  inject: ['reload'], //注入局部刷新
  data() {
    return {
      dataList: [],
      filters: {
        // company: "",
        institutionId: '',
        mchName: '',
        mchId: '',
      },
      page: {
        pageNo: 1,
        pageTotal: 0,
        pageSize: 20,
      },
      institutionList: [],
      listLoading: true,
      // pageLoading: false, // 页面加载loading

      permissionAdd: 'SYSTEM_MANAGER_FINANCE_MERCHANTNO_LIST_ADD',
      permissionCheck: 'SYSTEM_MANAGER_FINANCE_MERCHANTNO_LIST_SEE',
      permissionEdit: 'SYSTEM_MANAGER_FINANCE_MERCHANTNO_LIST_EDIT',
      permissionDelete: 'SYSTEM_MANAGER_FINANCE_MERCHANTNO_LIST_DELETE',
      permissionUpStatus: 'SYSTEM_MANAGER_FINANCE_MERCHANTNO_LIST_STATE',
    };
  },
  mounted() {
    this.init();
    window.addEventListener('keydown', this.keyDown);
  },
  destroyed() {
    window.removeEventListener('keydown', this.keyDown, false);
  },
  watch: {
    //to去哪，from从哪来
    $route(to, from) {
      if (from.path == '/finance/invoiceAdd') {
        this.listData();
      }
    },
  },
  methods: {
    init() {
      let _this = this;
      _this.listInstitution();
      _this.getTableHeight();
      _this.listData();
    },
    keyDown(e) {
      //如果是回车则执行查询方法
      if (e.keyCode == 13) {
        this.onSearch();
      }
    },

    listData() {
      let _this = this;
      _this.listLoading = true;
      var cond = {
        pageNo: _this.page.pageNo,
        pageSize: _this.page.pageSize,
        // company:_this.filters.company,
        institutionId: _this.filters.institutionId,
        mchId: _this.filters.mchId,
        mchName: _this.filters.mchName,
      };
      // 发送请求
      MerchantNoApi.listMerchantNo(cond)
        .then((data) => {
          this.dataList = data.data.list;
          this.page.pageTotal = data.data.total;
          _this.listLoading = false;
        })
        .catch((error) => {
          _this.listLoading = false;
          console.error(error);
        });
    },
    listInstitution() {
      let _this = this;
      InstitutionApi.selectInsByUser()
        .then((res) => {
          _this.institutionList = res.data.map((item) => {
            return {
              value: item.institutionId,
              label: item.institutionName,
            };
          });
        })
        .catch((error) => {
          _this.institutionList = [];
          console.error(error);
        });
    },
    getTableHeight() {
      this.$nextTick(function () {
        /* this.maxHeight =
                    window.innerHeight - this.$refs.tableRow.$el.offsetTop - 50;
                // 浏览器尺寸变化响应事件
                window.onresize = () => {
                    this.maxHeight =
                        window.innerHeight - this.$refs.tableRow.$el.offsetTop - 50;
                };*/
      });
    },
    // 过滤器
    formatState(cellValue) {
      if (cellValue == -1) {
        return '删除';
      } else if (cellValue == 0) {
        return '禁用';
      } else if (cellValue == 1) {
        return '启用';
      }
    },
    formatStates(cellValue) {
      if (cellValue == -1) {
        return 'danger';
      } else if (cellValue == 0) {
        return 'info';
      } else if (cellValue == 1) {
        return 'success';
      }
    },
    onSearch() {
      this.page.pageNo = 1;
      this.listData();
    },
    resetForm() {
      this.$refs.searchForm.resetFields();
      this.onSearch();
    },
    // 去新增页面
    handleClickAdd() {
      let _this = this;
      _this.$router.push('/finance/merchantNoAdd');
    },
    // 去编辑页面
    handleClickEditor(id) {
      this.$router.push({
        path: '/finance/merchantNoEdit',
        query: {
          id,
        },
      });
    },
    handleSizeChange(size) {
      let _this = this;
      _this.page.pageSize = size;
      _this.listData();
    },
    handleCurrentChange(val) {
      let _this = this;
      _this.page.pageNo = val;
      _this.listData();
    },
    handleIsDisable(row) {
      let _this = this;
      let currentState = row.state == 0 ? 1 : 0;
      this.$confirm(currentState ? '是否启用？' : '是否禁用', message.TITLE_PROMPT, {}).then(() => {
        MerchantNoApi.deleteMerchantNo({
          id: row.mchInfoId,
          state: currentState,
        }).then((res) => {
          this.$Utils.notifySuccess();
          this.listData();
        });
      });
    },
    handleClickDelete(row) {
      let _this = this;
      this.$confirm(message.MSG_CONFIRM_DELETE, message.TITLE_PROMPT, {}).then(() => {
        let cond = {
          id: row.mchInfoId,
          state: -1,
        };
        MerchantNoApi.deleteMerchantNo(cond).then((res) => {
          _this.listData();
          _this.$Utils.notifySuccess();
        });
      });
    },
  },
};
</script>

<style lang="scss" src="@/assets/css/views/content.scss" scope></style>
